//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.controller
{
	import __AS3__.vec.Vector;
	
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	import org.puremvc.as3.multicore.interfaces.ICommand;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.command.SimpleCommand;
	
	import reader.core.ReaderError;
	import reader.core.ReaderModelLocator;
	import reader.model.IDataProxy;
	import reader.model.IServiceProxy;
	import reader.model.vo.OPMLItemVO;
	import reader.model.vo.ServiceVO;
	import reader.utils.OPMLUtils;

	public class NewRegUserCommand extends SimpleCommand implements ICommand
	{
		//////////////////////////////////////////////////////////////////////////////
		//methods
	
		override public function execute(notification:INotification):void
		{
			var culture:String = ReaderModelLocator.instance.culture;
			
			//init data
			var dataProxy:IDataProxy = facade.retrieveProxy(ReaderModelLocator.instance.sqliteProxyFactory.dataProxyName) as IDataProxy;
			var file:File = File.applicationDirectory.resolvePath("resource/" + culture + ".opml");
			if(!file.exists)
				throw new ReaderError(ReaderError.OPML_ERROR);
			var stream:FileStream = new FileStream();
		    stream.open(file, FileMode.READ);
		    var fileData:String = stream.readUTFBytes(stream.bytesAvailable);
		    
		    try
		    {
		    	var xml:XML = new XML(fileData);
		    }
		    catch (error:Error)
			{
			    trace(error.message)
			}
			
			var list:Vector.<OPMLItemVO> = OPMLUtils.parseOPML(xml);
			for each(var item:OPMLItemVO in list)
			{
				dataProxy.addListSubscribe(item.subscribe, item.label)
			}
			
			//init service
			var serviceProxy:IServiceProxy = facade.retrieveProxy(ReaderModelLocator.instance.sqliteProxyFactory.serviceProxyName) as IServiceProxy;
			var sev:Vector.<ServiceVO> = createService(culture);
			for each(var vo:ServiceVO in sev)
			{
				vo.userId = ReaderModelLocator.instance.user.userId;
				vo.username = "";
				vo.password = "";
				vo.needAlert = false;
				vo.config = "";
				serviceProxy.addService(vo);
			}
			
		}
		
		private function createService(culture:String):Vector.<ServiceVO>
		{
			var file:File = File.applicationDirectory.resolvePath("resource/" + culture + ".sev");
			if(!file.exists)
				throw new ReaderError(ReaderError.OPML_ERROR);
			var stream:FileStream = new FileStream();
		    stream.open(file, FileMode.READ);
		    var fileData:String = stream.readUTFBytes(stream.bytesAvailable);
		    
		    try
		    {
		    	var xml:XML = new XML(fileData);
		    }
		    catch (error:Error)
			{
			    trace(error.message)
			}
			return ServiceVO.XMLToVector(xml);
			
		}
		
	}
}